package com.example.etwzgzxt.view.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.bean.Info;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class InfoUpdateActivity extends AppCompatActivity {

    /**
     * 选择文件
     */
    public static final int SELECT_AVATAR = 0;
    /**
     * 上传头像
     */
    public static final int UPDATE_AVATAR = 1;
    /**
     * 上传信息
     */
    public static final int UPDATE_INFO = 2;


    private String picPath = null;
    private Bitmap bm;
    private ProgressDialog progressDialog;

    private View.OnClickListener navListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            InfoUpdateActivity.this.finish();
        }
    };
    private Toolbar toolbar;
    private Info info;

    private TextView tvGenderMale, tvGenderFemale, btnSaveInfo;
    private EditText etRealName, etEmail, etQQ, etOccupation, etCompany, etAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_update);
        if (toolbar != null) {
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow_back);
            toolbar.setNavigationOnClickListener(navListener);
        }

        InfoApplication infoApplication = (InfoApplication) getApplication();
        info = infoApplication.getInfo();
        System.out.println(info.getRealName());

        initView();
    }

    public void initView() {
        tvGenderMale = (TextView) findViewById(R.id.tvGenderMale);
        tvGenderFemale = (TextView) findViewById(R.id.tvGenderFemale);
        etRealName = (EditText) findViewById(R.id.etRealName);
        etOccupation = (EditText) findViewById(R.id.etOccupation);
        etCompany = (EditText) findViewById(R.id.etCompany);
        etAddress = (EditText) findViewById(R.id.etAddress);
        etEmail = (EditText) findViewById(R.id.etEmail);
        etQQ = (EditText) findViewById(R.id.etQQ);
        btnSaveInfo = (TextView) findViewById(R.id.btnSaveInfo);
        btnSaveInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSaveInfo(v);
            }
        });

        try {
            etRealName.setText(info.getRealName());
            if (info.getGender().equals("男")) {
                btnGenderMale(null);
            } else {
                btnGenderFemale(null);
            }
            etOccupation.setText(info.getOccupation());
            etCompany.setText(info.getOccupation());
            etAddress.setText(info.getAddress());
            etEmail.setText(info.getEmail());
            etQQ.setText(info.getQQ());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public void btnSaveInfo(View view) {
        if (etRealName.getText() == null)
            return;
        progressDialog = ProgressDialog.show(this, null, null);
        if (info.getGender() == null)
            info.setGender("男");
        info.setCompany(etCompany.getText().toString());
        info.setOccupation(etOccupation.getText().toString());

        info.setRealName(etRealName.getText().toString());
        info.setAddress(etAddress.getText().toString());
        info.setEmail(etEmail.getText().toString());
        info.setQQ(etQQ.getText().toString());
        new BackgroundTask(UPDATE_INFO).execute();
    }

    public void btnGenderMale(View view) {
        tvGenderMale.setBackgroundResource(R.drawable.bg_info_gender_left);
        tvGenderMale.setTextColor(getResources().getColor(R.color.white));
        tvGenderFemale.setBackgroundDrawable(null);
        tvGenderFemale.setTextColor(getResources().getColor(R.color.colorPrimary));
        info.setGender("男");
    }

    public void btnGenderFemale(View view) {
        tvGenderFemale.setBackgroundResource(R.drawable.bg_info_gender_right);
        tvGenderFemale.setTextColor(getResources().getColor(R.color.white));
        tvGenderMale.setTextColor(getResources().getColor(R.color.colorPrimary));
        tvGenderMale.setBackgroundDrawable(null);
        info.setGender("女");
    }

    public class BackgroundTask extends AsyncTask<Void, Void, String> {

        int type;

        public BackgroundTask(int type) {
            this.type = type;
        }

        @Override
        protected String doInBackground(Void... params) {
                return updateInfo();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
            parseResult(result, type);
        }
    }

    public String updateInfo() {
        String baseURL = ((InfoApplication)getApplicationContext()).getBasehost()+"/user/profile/update.do";
        NameValuePair pair[] = new NameValuePair[9];
        pair[0] = new BasicNameValuePair("access_token",  ((InfoApplication) getApplication()).getAccess_token());
        pair[1] = new BasicNameValuePair("realname", info.getRealName());
        pair[2] = new BasicNameValuePair("gender", info.getGender());
        pair[3] = new BasicNameValuePair("occupation", info.getOccupation());
        pair[4] = new BasicNameValuePair("company", info.getCompany());// TODO: 2016/4/26
        pair[5] = new BasicNameValuePair("address", info.getAddress());
        pair[6] = new BasicNameValuePair("phone", info.getPhone());
        pair[7] = new BasicNameValuePair("email", info.getEmail());
        pair[8] = new BasicNameValuePair("qq", info.getQQ());
        List<NameValuePair> pairList = new ArrayList<>();
        Collections.addAll(pairList, pair);
        for (NameValuePair pairs: pairList) {
            System.out.println(pairs.getName()+" "+pairs.getValue());
        }
        return new HttpHelper().post(baseURL, pairList);
    }

    public void parseResult(String result, int type) {
        if (result == null) {
            Toast.makeText(this, "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    finish();
                    break;
                case 403:
                    Toast.makeText(this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
                case 406:
                    Toast.makeText(this, "Forbidden", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
